﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Text;

namespace TraditionalMalteseCuisine.App_Code.Objects
{
    [Serializable]
    public class Subscription : BaseTable
    {
        //*************************************************
        //                Class Members
        //*************************************************
        #region Class Members
        public const string TABLE_NAME = "Subscriptions";
        public const string VIEW_NAME_ALL_SUBSCRIPTIONS_DETAILS = "vwAll" + TABLE_NAME + "Details";
        public const string VIEW_NAME_SUBSCRIPTIONS_TOTAL_SUBSCRIPTIONS = "vw" + TABLE_NAME + "TotalSubscriptions";

        public const string MEMBER_ID_TAG = "member_" + SiteMember.ID_TAG;
        public const string SUBSCRIBED_TO_MEMBER_ID_TAG = "subscribed_to_member_" + SiteMember.ID_TAG;
        public const string POSTED_DATE_TAG = "posted_date";

        public const string TOTAL_SUBSCRIPTIONS_TAG = "TotalSubscriptions";

        private int mSubscribedToMemberID = -1;
        private int mMemberID = -1;
        private DateTime mPostedDate = DateTime.MinValue;
        #endregion
        //*************************************************


        //*************************************************
        //                 Constructor
        //*************************************************
        #region Constructor
        public Subscription() : this(-1, -1) { }

        public Subscription(int member_id, int subscribedToMemberID)
        {
            MemberID = member_id;
            SubscribedToMemberID = subscribedToMemberID;
        }
        #endregion
        //*************************************************


        //*************************************************
        //               Static Methods
        //*************************************************
        #region Static Methods
        public static OleDbCommand GetCommand_GetTopSubscribedMembers(int amount)
        {
            OleDbCommand oledbCommand = new OleDbCommand();
            oledbCommand.CommandType = CommandType.Text;

            StringBuilder commandText = new StringBuilder();
            commandText.AppendFormat("SELECT TOP {0} * FROM {1} ORDER BY {2} DESC, {3} ASC, {4} ASC", amount, VIEW_NAME_SUBSCRIPTIONS_TOTAL_SUBSCRIPTIONS, TOTAL_SUBSCRIPTIONS_TAG, SiteMember.FIRSTNAME_TAG, SiteMember.LASTNAME_TAG);

            oledbCommand.CommandText = commandText.ToString();

            return oledbCommand;
        }
        #endregion
        //*************************************************


        //*************************************************
        //                   Methods
        //*************************************************
        #region Methods
        protected override string GetInsertScriptUsingParameters()
        {
            StringBuilder insertScript = new StringBuilder();

            insertScript.Append("INSERT INTO ");
            insertScript.AppendFormat("{0} ", TABLE_NAME);
            insertScript.AppendFormat("([{0}], [{1}]) ", MEMBER_ID_TAG, SUBSCRIBED_TO_MEMBER_ID_TAG);
            insertScript.Append("VALUES ");
            insertScript.AppendFormat("(@{0}, @{1}) ", MEMBER_ID_TAG, SUBSCRIBED_TO_MEMBER_ID_TAG);

            return insertScript.ToString();
        }

        public override OleDbCommand GetInsertCommand()
        {
            OleDbCommand insertCommand = new OleDbCommand();

            insertCommand.CommandType = CommandType.Text;
            insertCommand.CommandText = GetInsertScriptUsingParameters();

            insertCommand.Parameters.AddWithValue(string.Format("@{0}", MEMBER_ID_TAG), ((MemberID > -1) ? (object)MemberID : DBNull.Value));
            insertCommand.Parameters.AddWithValue(string.Format("@{0}", SUBSCRIBED_TO_MEMBER_ID_TAG), ((SubscribedToMemberID > -1) ? (object)SubscribedToMemberID : DBNull.Value));

            return insertCommand;
        }

        public override BaseTable ReadDataRow(DataRow datarow)
        {
            //ID = int.Parse(datarow[ID_TAG].ToString());
            MemberID = int.Parse(datarow[MEMBER_ID_TAG].ToString());
            SubscribedToMemberID = int.Parse(datarow[SUBSCRIBED_TO_MEMBER_ID_TAG].ToString());
            PostedDate = DateTime.Parse(datarow[POSTED_DATE_TAG].ToString());
            //InUse = bool.Parse(datarow[IN_USE_TAG].ToString());

            return this;
        }


        public override string ToString()
        {
            return string.Format("{0} - {1}", MemberID, SubscribedToMemberID);
        }
        #endregion
        //*************************************************


        //*************************************************
        //                  Properties
        //*************************************************
        #region Properties
        public int MemberID
        {
            get { return mMemberID; }
            set { mMemberID = value; }
        }

        public int SubscribedToMemberID
        {
            get { return mSubscribedToMemberID; }
            set { mSubscribedToMemberID = value; }
        }

        public DateTime PostedDate
        {
            get { return mPostedDate; }
            set { mPostedDate = value; }
        }
        #endregion
        //*************************************************
    }
}